package com.zhengke.controller;

import java.io.File;
import java.io.IOException;

import com.zhengke.common.util.*;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.zhengke.common.object.AjaxDone;
import com.zhengke.common.object.Result;
import com.zhengke.service.BannerPhoneService;

@Controller
public class BannerPhoneController {
    /**
     * ssx
     */
    @Autowired
    private BannerPhoneService service;

    @RequestMapping("banner!toIndexSet.do")//用作跳转，到商城首页轮播图页面
    public String query(ModelMap mod) {
        //读取config.properties查找访问路径的ip+端口
        String fileServerUrl = null;
        try {
            fileServerUrl = LoadProperties.findValue("FILE_SERVER_URL");
        } catch (IOException e) {
            e.printStackTrace();
        }
        mod.put("fileServerUrl", fileServerUrl);
        return "bannerPhone/list";
    }

    @RequestMapping("Phone/queryPhone.do")
    @ResponseBody
    public Result queryPhone(Integer imgClass) {
        Result rs = null;
        imgClass = 0;//选择图片种类
        try {
            rs = service.queryPhones(imgClass);
            return rs;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @RequestMapping("phone/update.do")//用作跳转，到修改轮播图页面
    public String querys(String picPath, Integer id, ModelMap mod) {
        //System.out.println(id+"-----1----"+picPath);
        mod.put("id", id);
        mod.put("picPath", picPath);
        //读取config.properties查找访问路径的ip+端口
        String fileServerUrl = null;
        try {
            fileServerUrl = LoadProperties.findValue("FILE_SERVER_URL");
        } catch (IOException e) {
            e.printStackTrace();
        }
        mod.put("fileServerUrl", fileServerUrl);
        return "bannerPhone/XW002";
    }


    /**
     * 修改图片信息
     */
    @ResponseBody
    @RequestMapping(value = "phone/updatePhones.do", method = RequestMethod.POST)
    public AjaxDone updateHomePic(String picPath, Integer id, String fileId) {
        Result rs = null;
		/*System.out.println(picPath+"-------------1");
		System.out.println(id+"-------------2");*/
        try {
            rs = service.updatePhones(id, picPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String s = rs.getCode();
        int i = Integer.valueOf(s).intValue();
        if (i > 0) {
            //删除之前上传到fastdfs的图片
            FastDfsApiOpr.deleteFile(fileId);
            return new AjaxDone("200", "success");
        }

        return new AjaxDone("300", "bad");
    }


    /**
     * 上传原始图片
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("phone/uploadPhone.do")
    public AjaxDone updateImage(@RequestParam("image") CommonsMultipartFile image) {
        try {
            String imageFileName = image.getOriginalFilename();

            if (imageFileName.indexOf(".jpg") == -1
                    && imageFileName.indexOf(".gif") == -1
                    && imageFileName.indexOf(".png") == -1
                    && imageFileName.indexOf(".jpeg") == -1) {
                return new AjaxDone("300", "文件格式不正确，请上传 *.png,*.jpeg,*.gif,*.jpg类型图片");
            }

            String extName = imageFileName.substring(imageFileName.lastIndexOf(".") + 1);
            if (image != null && !image.isEmpty()) {
                String path = LoadProperties.findValue("imgPath") + "/" + DateUtil.getSystemDateTimeByFormat("yyyyMMddHHmmss") + "." + extName;
                File file = new File(path);
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                FileUtils.copyInputStreamToFile(image.getInputStream(), file);
                String rtnPath = LoadProperties.findValue("viewUrl") + "?path=" + file.getPath();

                return new AjaxDone("200", "上传成功", rtnPath, path);
            } else {
                return new AjaxDone("300", "文件格式不正确，请上传 *.png,*.jpeg,*.gif,*.jpg类型图片");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxDone("300", "上传失败");
    }

    /**
     * 上传剪切后的图片
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("phone/uploadCanvas.do")
    public AjaxDone updateConvars(String gcId, String canvasData, String delFile) {
        try {
            String path = LoadProperties.findValue("imgPath") + "/";
            String fileName = DateUtil.getSystemDateTimeByFormat("yyyyMMddHHmmss") + ".jpg";

            File file = Base64ToImage.decodeBase64ToImage(canvasData, path, fileName);

            //删除原图片
            File delete = new File(delFile);
            if (delete != null) {
                delete.delete();
            }

            if (file != null) {
                String imgW = LoadProperties.findValue("imgW");
                String imgH = LoadProperties.findValue("imgH");
                //保存canvas转成的缩略图
                String filePath = ImageUtil.thumbnailImage(file.getPath(), Integer.parseInt(imgW), Integer.parseInt(imgH), true);
                //删除canvas原图
                file.delete();

                /*
                fastDFS操作
                 */
                //获取文件的扩展名
                String filename = fileName;
                //获取文件后缀名 从最后一个点后一位截取
                String extName = fileName.substring(filename.lastIndexOf(".") + 1);
                //上传fastDFS后图片完整地址
                String file_id = FastDfsApiOpr.upload(filePath, extName);
                //读取config.properties查找访问路径的ip+端口
                String rtnPath = LoadProperties.findValue("FILE_SERVER_URL") + file_id;
                System.out.println(file_id);
                System.out.println(rtnPath);
                //删除剪切后的本地图片
                File delCanvas = new File(filePath);
                if (delCanvas != null) {
                    delCanvas.delete();
                }
                return new AjaxDone("200", "上传成功!", rtnPath, file_id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxDone("300", "系统繁忙,请稍后重试!");
    }


}
